//
//  DKLoginPhoneView.m
//  DanKU
//
//  Created by 吴斌 on 2023/9/20.
//

#import "DKLoginPhoneView.h"
@interface DKLoginPhoneView()<UITextFieldDelegate>
@property (nonatomic, strong)UILabel* dk_RegisterPhoneplaceLab;
@property (nonatomic, strong)UIButton * dk_RegisterPhoneClearBtn;
@end
@implementation DKLoginPhoneView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = DK_Hex(@"#F7F7F8");
        self.layer.borderWidth = 0;
        self.layer.borderColor = DK_Hex(@"#4566FF").CGColor;
        self.layer.cornerRadius = 14;
        self.clipsToBounds = YES;
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    UILabel *dk_RegisterPhoneHeadLab = [[UILabel alloc] init];
    dk_RegisterPhoneHeadLab.textColor = DK_Hex(@"#333333");
    dk_RegisterPhoneHeadLab.font = DK_FONT_Regular(15);
    dk_RegisterPhoneHeadLab.text = @"+91";
    [self addSubview:dk_RegisterPhoneHeadLab];
    [dk_RegisterPhoneHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(20)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
    self.dk_RegisterPhoneTextField = [[UITextField alloc] init];
    self.dk_RegisterPhoneTextField.textColor = DK_Hex(@"#333333");
    self.dk_RegisterPhoneTextField.font = DK_FONT_Regular(15);
    self.dk_RegisterPhoneTextField.delegate = self;
    self.dk_RegisterPhoneTextField.backgroundColor = UIColor.clearColor;
    self.dk_RegisterPhoneTextField.keyboardType = UIKeyboardTypeASCIICapableNumberPad;
    [self addSubview:self.dk_RegisterPhoneTextField];
    [self.dk_RegisterPhoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(dk_RegisterPhoneHeadLab.mas_right).offset(DK_IsWidthScale(2));
        make.right.equalTo(@(-DK_IsWidthScale(34)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
    }];
    self.dk_RegisterPhoneplaceLab = [[UILabel alloc] init];
    self.dk_RegisterPhoneplaceLab.textColor = DK_Hex(@"#CCCCCC");
    self.dk_RegisterPhoneplaceLab.font = DK_FONT_Regular(15);
    self.dk_RegisterPhoneplaceLab.text = @"Please enter your registered cell phone number";
    self.dk_RegisterPhoneplaceLab.numberOfLines = 2;
    [self.dk_RegisterPhoneTextField addSubview:self.dk_RegisterPhoneplaceLab];
    [self.dk_RegisterPhoneplaceLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(DK_IsWidthScale(0)));
    }];
    
    self.dk_RegisterPhoneClearBtn = [[UIButton alloc] init];
    [self.dk_RegisterPhoneClearBtn setImage:DK_GetImage(@"dk_LoginPhoneClear") forState:UIControlStateNormal];
    self.dk_RegisterPhoneClearBtn.hidden = YES;
    [self.dk_RegisterPhoneClearBtn addTarget:self action:@selector(clearPhone) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.dk_RegisterPhoneClearBtn];
    [self.dk_RegisterPhoneClearBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DK_IsWidthScale(10)));
        make.width.equalTo(@(DK_IsWidthScale(20)));
        make.height.equalTo(@(DK_IsWidthScale(20)));
        make.centerY.equalTo(self);
    }];
}
- (void)clearPhone{
    self.dk_RegisterPhoneTextField.text = @"";
    self.dk_RegisterPhoneplaceLab.hidden = NO;
    self.dk_RegisterPhoneClearBtn.hidden = YES;
    self.layer.borderWidth = 0;
}
- (void)setLoginPlace:(NSString *)loginPlace{
    _loginPlace = loginPlace;
    self.dk_RegisterPhoneplaceLab.text = _loginPlace;
}
- (NSString *)getLoginPhone{
    return self.dk_RegisterPhoneTextField.text;
}
#pragma mark UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{//开始编辑
    if(self.StartEditPhoneBlock){
        self.StartEditPhoneBlock();
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * dk_RegisterPhoneText = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if(dk_RegisterPhoneText.length > 0){
        self.dk_RegisterPhoneplaceLab.hidden = YES;
        self.dk_RegisterPhoneClearBtn.hidden = NO;
        self.layer.borderWidth = 0.5;
    }else{
        self.dk_RegisterPhoneplaceLab.hidden = NO;
        self.dk_RegisterPhoneClearBtn.hidden = YES;
        self.layer.borderWidth = 0;
    }
    if(dk_RegisterPhoneText.length > 0){
        if(self.goCheckPhoneBlock){
            self.goCheckPhoneBlock(false);
        }
    }else{
        if(self.goCheckPhoneBlock){
            self.goCheckPhoneBlock(true);
        }
    }
    return YES;
}
@end
